import { _decorator, Component, instantiate, Label, Node, Prefab, Sprite, v3 } from 'cc';
import { Condition, Tech } from '../entity/Tech';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Soldier } from '../entity/Soldier';
import { ConItem } from './ConItem';
import { buildingType } from '../settings/const';
import { Utils } from '../common/Utils';
import StudyApi from '../api/StudyApi';
import { UserInfo } from '../common/UserInfo';
import { Advanced } from '../entity/Advanced';
import { TrainProperty } from '../train/TrainProperty';
const { ccclass, property } = _decorator;

@ccclass('UpgradeTech')
export class UpgradeTech extends Component {

    @property(Node)
    bg: Node
    @property(Node)
    close: Node
    @property(Label)
    title: Label
    @property(Sprite)
    cover: Sprite
    @property(Sprite)
    border: Sprite
    @property(Label)
    soldierName: Label
    @property(Label)
    curAttack: Label
    @property(Label)
    curLevel: Label
    @property(Label)
    nextLevel: Label
    @property(Label)
    nextAttack: Label

    @property(Node)
    conList: Node
    @property(Label)
    food: Label
    @property(Label)
    wood: Label
    @property(Label)
    mud: Label
    @property(Label)
    iron: Label
    @property(Label)
    time: Label

    @property(Node)
    cancel: Node
    @property(Node)
    upgrade: Node
    @property(Node)
    lookPro: Node
    @property(Node)
    canNot: Node
    @property(Node)
    btnPos: Node
    @property(Label)
    canNotLabel:Label
    @property(Label)
    resLabel:Label
    @property(Node)
    resLayout:Node

    @property(Prefab)
    conItem: Prefab

    techData: Tech
    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.cancel.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.bg.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.upgrade.on(Node.EventType.TOUCH_END, this.onUpgrade, this)
        this.lookPro.on(Node.EventType.TOUCH_END, this.onLookPro, this)
    }

    async setData(tech: Tech) {
        this.techData = tech
        this.cover.spriteFrame = await tech.getTechImg()
        this.soldierName.string = tech.name
        this.title.string = tech.name + '强化'

        if (tech.id > 40) {
            // 建筑科技

        } else {
            // 兵种科技
            this.setSoldier()


        }
    }

    setSoldier() {
        let soldier = Soldier.createById(this.techData.id)
        soldier.attackLevel = this.techData.level
        let ad = soldier.getAttackAdvanced()
        console.log(ad)
        this.curAttack.string = (ad.addition * 100).toFixed(1) + '%'
        this.curLevel.string = '当前等级' + ad.level
        this.nextAttack.string = (ad.additionNext * 100).toFixed(1) + '%'
        this.nextLevel.string = '下一等级' + (ad.level + 1)

        // 研究条件
        let node = instantiate(this.conItem)
        let condi = Condition.create({type:'b', lv:ad.level + 1, id:buildingType.B22_Academy})
        node.getComponent(ConItem).setData(condi)
        this.conList.addChild(node)

        this.food.string = ad.food + ''
        this.mud.string = ad.mud + ''
        this.wood.string = ad.wood + ''
        this.iron.string = ad.iron + ''
        this.time.string = Utils.secondToStr(ad.time) 

       

        this.showBtns(ad)
        
    }

    showBtns(ad:Advanced){
        this.lookPro.position = v3(-150,this.btnPos.position.y, 0)
        // 满级的情况
        if(this.techData.level >= this.techData.max){
            this.resLabel.node.active = false
            this.resLayout.active = false
            this.canNotLabel.string = '已经满级了'
            this.canNot.position = v3(150,this.btnPos.position.y, 0)
            return
        }

        // 判断是否有其他的科技正在研究
        let eventList = Tech.getTechStudyEventList()
        if(eventList.length > 0){
            this.canNotLabel.string = '正在进行其他研究'
            this.canNot.position = v3(150,this.btnPos.position.y, 0)
            return
        }

        

        if(!ad.isEnoughBuilding){
           
            this.canNot.position = v3(150,this.btnPos.position.y, 0)
            this.canNotLabel.string = '需要升级研究院'
            return
        }
        if(!ad.isEnoughBuilding){
           
            this.canNot.position = v3(150,this.btnPos.position.y, 0)
            this.canNotLabel.string = '资源不足'
            return
        }


        
        this.upgrade.position = v3(150,this.btnPos.position.y, 0)
    }

    async onUpgrade() {
        await UIManager.instance.showLoading()
        let res = await StudyApi.studying({
            a: this.techData.id
        })
        UIManager.instance.closeLoading()
        this.onClose()
        UserInfo.instance.refreshVillageDetail()
    }


    onClose() {
        UIManager.instance.closeUI(PrefabEnum.UpgradeTech)
    }

    async onLookPro(){
        let ui = await UIManager.instance.openUI(PrefabEnum.TrainProperty)
        
        let soldier = Soldier.createById(this.techData.id)
        soldier.attackLevel = this.techData.level
        
        ui.getComponent(TrainProperty).setData(soldier)
    }


}


